package com.guge.sports.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guge.sports.entity.Dto.DeptRunDetailDto;
import com.guge.sports.entity.Dto.RunningDetailsDataDto;
import com.guge.sports.entity.Dto.SchoolRunDetailDto;
import com.guge.sports.entity.RunSum;
import com.guge.sports.entity.StDept;
import com.guge.sports.mapper.RunSumMapper;
import com.guge.sports.mapper.StDeptMapper;
import com.guge.sports.mapper.StSchoolMapper;
import com.guge.sports.service.RunSumService;
import com.guge.sports.toolPackage.base.BaseService;
import com.guge.sports.entity.Dto.StudentRunningDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-09-14
 */
@Service
public class RunSumServiceImpl extends BaseService<RunSum> implements RunSumService {
    @Autowired
    private RunSumMapper runSumMapper;

    @Autowired
    private StSchoolMapper stSchoolMapper;

    @Autowired
    private StDeptMapper stDeptMapper;

    /**
     * 各学校下学院的跑步情况
     * @return
     */
    public List<SchoolRunDetailDto> getSchoolRunDetails(DeptRunDetailDto deptRunDetailDto){
        List<SchoolRunDetailDto> schoolRunDetailList = stSchoolMapper.getSchoolNameAndId();
        // 遍历学校
        for (SchoolRunDetailDto schoolRunDetailDto : schoolRunDetailList) {
            Long scId = schoolRunDetailDto.getScId();
            QueryWrapper<StDept> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("did","dept_name").eq("school_id",scId);
            List<StDept> depts = stDeptMapper.selectList(queryWrapper);
            List<DeptRunDetailDto> deptRunDetails = new ArrayList<>();
            // 遍历各学院
            for (StDept dept : depts) {
                DeptRunDetailDto deptRunDetail = runSumMapper.getDeptRunDetail(dept.getDid(),deptRunDetailDto);
                if (deptRunDetail.getValidCount() != null){
                    deptRunDetail.setDeptId(dept.getDid());
                    deptRunDetail.setDeptName(dept.getDeptName());
                    deptRunDetail.setValidCountAvg( (deptRunDetail.getValidCount()*1.0)/ deptRunDetail.getJoinNum());
                    deptRunDetail.setValidMileAvg(deptRunDetail.getValidCountMile()/deptRunDetail.getJoinNum());
                    deptRunDetails.add(deptRunDetail);
                }
            }
            schoolRunDetailDto.setDeptRunDetailDtos(deptRunDetails);
        }
        return schoolRunDetailList;
    }

    @Override
    public Page<StudentRunningDataDto> StudentRunningDataList(Page<StudentRunningDataDto> page, StudentRunningDataDto studentRunningDataDto) {
        return runSumMapper.StudentRunningDataList(page , studentRunningDataDto);
    }

    @Override
    public Page<RunningDetailsDataDto> RunningDetailsDataList(Page<RunningDetailsDataDto> page, RunningDetailsDataDto runningDetailsDataDto) {
        return runSumMapper.RunningDetailsDataList(page , runningDetailsDataDto);
    }
}
